/**
 * 封装交易市场合约
 */
import {market_address} from "@/config/contract";
import {ethers} from "ethers";
import abi from "../abis/market.json"

let web3Provider = new ethers.BrowserProvider(window.ethereum)
//构造合约实例(地址，ABI，签名)
const contract = new ethers.Contract(market_address,abi,await web3Provider.getSigner())

//封装市场中的方法

//上架
/**
 * 无法调用：internal的方法
 */
export async function place(
    _seller,
    _tokenId,
    _price
){
    const result = await contract.placeOrder(_seller,_tokenId,_price);
    console.log('place:', result.hash);
}
//购买
export async function buy(
    tokenId){
    return await contract.buy(tokenId);
}
//改价
export async function changePrice(
    tokenId,
    price) {
    return await contract.changePrice(tokenId, price);
}

//下架
export async function cancelOrder(
    tokenId) {
    const result = await contract.cancelOrder(tokenId);
    console.log('cancel order', result.hash);
}

//获得市场中所有的订单
export async function getAllNFTs() {
    return await contract.getAllNFTs()
}

//获得自己所有上架的nft
export async function getMyNFTs() {
    return await contract.getMyNFTs();
}

//查看某个在售的nft的订单详细信息
export async function getOrder(
    tokenId) {
    const result = await contract.orderOfId(tokenId);
    return {
        seller: result[0],
        tokenId: Number(result[1]),
        price: Number(result[2]) / 1e18,
    }
}